﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using ASPNET.WebControls;
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Entities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.SaleSystem.Tags;
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Data;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;
    using Hidistro.ControlPanel.Commodities;
    using JSShop.Model;
    using JSShop.Enum;

    [ParseChildren(true)]
    public class SubCategory_new : HtmlTemplatedWebControl
    {
        private Common_Location common_Location;
        private wnxd_Shopping_LiList GoodsList;
        private wnxd_Pager Pager;
        private HtmlGenericControl divSpread;
        private int categoryId;
        private Hidistro.Entities.Commodities.CategoryInfo category;
        public SubCategory_new()
        {
            if (int.TryParse(this.Page.Request.QueryString["CategoryId"], out this.categoryId))
            {
                category = CategoryBrowser.GetCategory(this.categoryId);
                if (category == null)
                {
                    this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该分类不存在"));
                }
                else
                {
                    if (category.ParentCategoryId == 0 && this.Page.Request.RawUrl.IndexOf("?") == -1)
                    {
                        this.Page.Server.Transfer("SubPage_new.aspx", true);
                    }
                    else if (category.Depth == 1 && !(string.IsNullOrEmpty(category.Theme) || !File.Exists(HiContext.Current.Context.Request.MapPath(HiContext.Current.GetSkinPath() + "/categorythemes/" + category.Theme))))
                    {
                        this.SkinName = "/categorythemes/" + category.Theme;
                    }
                }
            }
            else if (!string.IsNullOrEmpty(this.Page.Request.QueryString["rewrite"]))
            {
                category = CategoryBrowser.GetCategoryByRewrite(this.Page.Request.QueryString["rewrite"]);
                if (category != null)
                {
                    this.categoryId = category.CategoryId;
                    if (string.IsNullOrEmpty(this.Page.Request.QueryString["toprewrite"]))
                    {
                        if (category.ParentCategoryId == 0 && this.Page.Request.RawUrl.IndexOf("?") == -1)
                        {
                            string path = this.MapPathSecure("~/" + this.Page.Request.QueryString["rewrite"] + "/index.html");
                            if (File.Exists(path))
                            {
                                this.Page.Response.WriteFile(path);
                                this.Page.Response.End();
                            }
                            else
                            {
                                this.Page.Server.Transfer("SubPage_new.aspx?CategoryId=" + this.categoryId, true);
                            }
                        }
                    }
                    else
                    {
                        if (category.ParentRewriteName != this.Page.Request.QueryString["toprewrite"])
                        {
                            this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该分类不存在"));
                        }
                    }
                    this.Page.Server.Transfer("SubCategory.aspx?CategoryId=" + this.categoryId + "&" + this.Page.Request.QueryString.ToString(), true);
                }
                else
                {
                    this.Page.Response.Redirect(Globals.ApplicationPath + "/ResourceNotFound.aspx?errorMsg=" + Globals.UrlEncode("该分类不存在"));
                }
            }
        }
        protected override void AttachChildControls()
        {
            this.common_Location = (Common_Location)this.FindControl("common_Location");
            this.GoodsList = (wnxd_Shopping_LiList)this.FindControl("GoodsList");
            this.divSpread = (HtmlGenericControl)this.FindControl("spread");
            this.Pager = (wnxd_Pager)this.FindControl("pager");

            if (!this.Page.IsPostBack)
            {
                if (this.category != null)
                {
                    if (this.common_Location != null)
                    {
                        this.common_Location.CateGoryPath = this.category.Path;
                    }
                    this.LoadPageSearch();
                }
                this.Bind();
            }
        }

        private void ShowSpread(int i)
        {
            switch (i)
            {
                case 1:
                    if (!string.IsNullOrEmpty(category.Notes1)) divSpread.InnerHtml = category.Notes1;
                    break;
                case 2:
                    if (!string.IsNullOrEmpty(category.Notes2)) divSpread.InnerHtml = category.Notes2;
                    break;
                case 3:
                    if (!string.IsNullOrEmpty(category.Notes3)) divSpread.InnerHtml = category.Notes3;
                    break;
            }
        }
        private void Bind()
        {
            ProductBrowseQuery productBrowseQuery = this.GetProductBrowseQuery();
            JSShop.Model.DbQueryResult browseProductList = new Juse.Interface.ProductBrowser().GetSearchsProductByCategoryId(productBrowseQuery);//ProductBrowser.GetBrowseProductList(productBrowseQuery);

            if (browseProductList == null)
                return;
            foreach (JSShop.Model.vw_Hishop_BrowseProductList model in browseProductList.Data)
            {
                if (model.EndDate != null)
                {
                    model.Times = (model.EndDate.Value.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds.ToString();
                }
                else
                {
                    model.Times = (DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds.ToString();
                }
            }

            this.GoodsList.DataSource = browseProductList.Data;
            this.GoodsList.DataBind();
            this.Pager.Total = browseProductList.TotalRecords;
            int cid = 0;
            if (this.category != null) cid = this.category.TopCategoryId;

        }
        private ProductBrowseQuery GetProductBrowseQuery()
        {
            int num2;
            ProductBrowseQuery entity = new ProductBrowseQuery();
            if (int.TryParse(this.Page.Request.QueryString["categoryId"], out num2))
            {
                entity.CategoryId = num2;
            }
            if (int.TryParse(this.Page.Request.QueryString["brand"], out num2))
            {
                entity.BrandId = num2;
            }
            if (int.TryParse(this.Page.Request.QueryString["feature"], out num2))
            {
                entity.feature = num2;
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["keywords"]))
            {
                entity.Keywords = DataHelper.CleanSearchString(Globals.HtmlEncode(Globals.UrlDecode(this.Page.Request.QueryString["keywords"])));
            }
            if (int.TryParse(this.Page.Request.QueryString["filter"], out num2))
            {
                switch (num2)
                {
                    case 1:
                        {
                            entity.MaxSalePrice = 100;
                            break;
                        }
                    case 2:
                        {
                            entity.MinSalePrice = 100;
                            entity.MaxSalePrice = 200;
                            break;
                        }
                    case 3:
                        {
                            entity.MinSalePrice = 200;
                            entity.MaxSalePrice = 300;
                            break;
                        }
                    case 4:
                        {
                            entity.MinSalePrice = 300;
                            entity.MaxSalePrice = 500;
                            break;
                        }
                    case 5:
                        {
                            entity.MinSalePrice = 500;
                            break;
                        }
                }
            }
            entity.PageIndex = this.Pager.Index;
            entity.PageSize = this.Pager.Size;
            if (int.TryParse(this.Page.Request.QueryString["rank"], out num2))
            {
                switch (num2)
                {
                    case 1:
                        {
                            entity.SortBy = "ShowSaleCounts";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 2:
                        {
                            entity.SortBy = "ShowSaleCounts";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 3:
                        {
                            entity.SortBy = "VistiCounts";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 4:
                        {
                            entity.SortBy = "VistiCounts";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 5:
                        {
                            entity.SortBy = "SalePrice";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 6:
                        {
                            entity.SortBy = "SalePrice";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                    case 7:
                        {
                            entity.SortBy = "AddedDate";
                            entity.SortOrder = SortAction.Desc;
                            break;
                        }
                    case 8:
                        {
                            entity.SortBy = "AddedDate";
                            entity.SortOrder = SortAction.Asc;
                            break;
                        }
                }
            }
            else
            {
                entity.SortBy = "DisplaySequence";
            }
            Globals.EntityCoding(entity, true);
            return entity;
        }

        private void LoadPageSearch()
        {
            string filter = this.Page.Request.QueryString["filter"] == null ? "" : this.Page.Request.QueryString["filter"].ToString();
            string brand = this.Page.Request.QueryString["brand"] == null ? "" : this.Page.Request.QueryString["brand"].ToString();
            string feature = this.Page.Request.QueryString["feature"] == null ? "" : this.Page.Request.QueryString["feature"].ToString();
            string rank = this.Page.Request.QueryString["rank"] == null ? "0" : this.Page.Request.QueryString["rank"].ToString();
            if (string.IsNullOrEmpty(filter + brand + feature))
            {
                if (!string.IsNullOrEmpty(this.category.MetaKeywords))
                {
                    MetaTags.AddMetaKeywords(this.category.MetaKeywords, HiContext.Current.Context);
                }
                if (!string.IsNullOrEmpty(this.category.MetaDescription))
                {
                    MetaTags.AddMetaDescription(this.category.MetaDescription, HiContext.Current.Context);
                }
                if (!string.IsNullOrEmpty(this.category.MetaTitle))
                {
                    PageTitle.AddSiteNameTitle(this.category.MetaTitle);
                }
                else
                {
                    PageTitle.AddSiteNameTitle(this.category.Name);
                }
            }
            else
            {
                //橘色 制服诱惑 护士装 全部 综合排序产品 第1页 【橘色商城】
                if (!string.IsNullOrEmpty(feature))
                {
                    List<JSShop.Model.wnxd_feature> featurelist = new Juse.Interface.CategoryBrowser().GetFeature(category.CategoryId.ToString());
                    string[] wnxd_feature = null;
                    if (featurelist.Count > 0)
                    {
                        wnxd_feature = featurelist[0].Value.Split(',');
                    }
                    else
                    {
                        feature = "";
                    }

                    if (wnxd_feature != null)
                    {
                        feature = wnxd_feature[int.Parse(feature)-1];
                    }
                    else
                    {
                        feature = "";
                    }

                }
                else
                {
                    feature = "情趣用品";
                }

                if (!string.IsNullOrEmpty(brand))
                {
                    List<JSShop.Model.Hishop_BrandCategories> brandCategories = new Juse.Interface.CategoryBrowser().GetBrandList(this.categoryId, 16);
                    foreach (Hishop_BrandCategories v in brandCategories)
                    {
                        if (v.BrandId == int.Parse(brand))
                        {
                            brand = v.BrandName;
                            break;
                        }
                    }
                }
                else
                {
                    brand = "橘色";
                }
                if (!string.IsNullOrEmpty(filter))
                {
                    switch (filter)
                    {
                        case "1":
                            {
                                filter = "100元及以下";
                                break;
                            }
                        case "2":
                            {
                                filter = "101-200元";
                                break;
                            }
                        case "3":
                            {
                                filter = "201-300元";
                                break;
                            }
                        case "4":
                            {
                                filter = "301-500元";
                                break;
                            }
                        case "5":
                            {
                                filter = "500元以上";
                                break;
                            }
                        case "0":
                            {
                                filter = "全部";
                                break;
                            }
                    }
                }
                switch (rank)
                {
                    case "0":
                        {
                            rank = "综合排序";
                            break;
                        }
                    case "1":
                        {
                            rank = "销量优先";
                            break;
                        }
                    case "3":
                        {
                            rank = "人气优先";
                            break;
                        }
                    case "6":
                        {
                            rank = "价格最低";
                            break;
                        }
                    case "5":
                        {
                            rank = "价格最高";
                            break;
                        }
                    case "7":
                        {
                            rank = "最新上架";
                            break;
                        }
                }

                string name = brand + feature + category.Name + filter + rank + "产品第" + this.Pager.Index.ToString() + "页";

                MetaTags.AddMetaKeywords(name, HiContext.Current.Context);
                MetaTags.AddMetaDescription(name + this.category.MetaDescription, HiContext.Current.Context);
                PageTitle.AddSiteNameTitle(name);

            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-SubCategory_new.html";
            }
            base.OnInit(e);
        }
    }
}


